/*
 */
package ImportDataFromPhone;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author ash
 */
public class RawRecordParser {
    
    private static String DEFAULT_DATE="0";
    private static String DEFAULT_VALUE="0";
    
    //regexp patterns
    private static Pattern isDate = Pattern.compile("^ *(\\d+\\.\\d+) *$");
    private static Pattern isCommonRecord = Pattern.compile("^(.*) +(\\d+)$");

    private Container<RawRecord> input;
    private Container<ProcessedRecord> output;
    
    private RawRecordParser (Container<RawRecord> in){
        input = in;
        output = new Container<>(ProcessedRecord.class);
    }
    
    static Container<ProcessedRecord> process(Container<RawRecord> records){
        RawRecordParser rt = new RawRecordParser(records);
        rt.transformation();
        return rt.output;
    }

    private void transformation(){
        String currentDate=DEFAULT_DATE;
        Matcher m;
        String str;
        
        for (int i=0; i<input.size(); i++){
            str=input.get(i).toString();
            m=isDate.matcher(str);
            if (m.matches()){
                currentDate=m.group();
            } else {
                m=isCommonRecord.matcher(str);
                if (m.matches()){
                    output.add(new ProcessedRecord(m.group(1), m.group(2), currentDate));
                } else {
                    output.add(new ProcessedRecord(str, DEFAULT_VALUE, currentDate));
                }
            }
        }
        
    }
   
}
